<!-- 资源共享 -->
<template>
  <div class="dashboard-editor-container">
    <el-row style="background: #fff; padding: 16px">
      <el-row :gutter="24">
        <div style="display:flex;align-items:center;">
          <div style="width:100%">
            <el-col :xs="12" :sm="12" :lg="6"> 
              <el-input v-model="Filter" placeholder="请输入资料名称" clearable></el-input>
            </el-col>
            <el-col :xs="8" :sm="8" :lg="8"> 
              <el-button type="primary" @click="search">搜索</el-button>
            </el-col>
          </div>
          <div style="width:200px;color:#2491fc;">每日最大下载 5 个文件</div>
        </div>
      </el-row>
      <el-tabs type="border-card" style="margin-top: 16px" @tab-click="handleClick">
        <el-tab-pane v-for="(item, index) in tablist" :key="index" :label="item" lazy>
          <el-col :xs="24" :sm="24" :lg="24"> 
            <el-table stripe border :data="tableData" style="width: 100%; margin-top: 15px" height="620">
              <el-table-column label="序号"  width="60" type="index" :index='(index)=>{return (index + 1) + (pageNum - 1)*pageSize}'> </el-table-column>
              <el-table-column label="资料名称" prop="FileName" width="600px" align="left" show-overflow-tooltip></el-table-column>
              <el-table-column label="资料尺寸" prop="FileSize" width="120px" align="left"></el-table-column>
              <el-table-column label="操作"  :width="Dir === '海关数据' ? '220px' : '120px'">
                <template slot-scope="scope">
                  <el-button type="primary" size="mini" icon='el-icon-download' @click="download(scope.row.ShortFileName)">下载</el-button>
                  <!-- <el-button type="primary" size="mini" icon='el-icon-cpu' v-show="Dir === '海关数据'" @click="aiAnalysis(scope.row.FileName)">邮箱挖掘</el-button> -->
                </template>
              </el-table-column>
              <el-table-column ></el-table-column>
            </el-table>
          </el-col>
        </el-tab-pane>
      </el-tabs>
    </el-row>
  </div>
</template>

<script>
import { connrequest, download_token } from '@/api/firstPage'
import { bytesToSize, objectMerge } from '@/utils/index.js'
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      tablist: [],
      typeData: [],
      Dir:'',
      Filter: ''
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getData()
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
  },
  methods: {
    async getData() {
      const res = await connrequest('GetShareDirs')
      let tablist = []
      res.Data = res.Data || []
      res.Data.map(item => {
        tablist.push(item)
      })
      this.Dir = tablist[0]
      this.GetShareFiles(this.Dir)
      this.tablist = new Set(tablist)
    },
    async GetShareFiles(Dir){
      const data = {
        Data:{
          Dir: Dir,
          Filter: this.Filter
        }
      }
      const res = await connrequest('GetShareFiles', data)
      res.Data = res.Data || []
      res.Data.map(item =>{
        item.FileSize = bytesToSize(item.FileSize)
        return item
      })
      this.tableData = res.Data
    },
    search(){
      this.GetShareFiles(this.Dir)
    },
    async download(row){
      // DownloadPublicFile
      // if(res.Data.UserLevel === '普通会员'){
      //   this.$message.info('会员等级没有权限，需要升级才能下载资料')
      //   return false
      // }else 
      // if(sessionStorage.getItem('username') === '游客'){
      //   this.$message.info('很抱歉，您的角色没有权限下载资料。如果需要下载，请联系管理员获取相应权限。')
      //   return false
      // }else{
        const arr = row.split('/');
        const FileName = arr[arr.length - 1];
        // const res = await connrequest('GetTodayDownloadResCount')
        // if(res?.Data?.Count < 5){
          const data = {
            Data: {
              FileName:  row
            }
          }
          const file = await download_token('DownloadPublicFile', data)
          if(file.byteLength === 80){
            this.$message.error('没有权限')
            return false
          }
          let blobUrl = window.URL.createObjectURL(new Blob([file]));
          const a = document.createElement('a');
          a.style.display = 'none';
          a.download = FileName;
          a.href = blobUrl;
          a.click();
          // await connrequest('AddTodayDownloadResCount')
        // }else{
          // this.$message.info('今日已到限制次数，请明日再下载！')
        // }

      // }
    },
    async aiAnalysis(row){
      const data = {
        Data:{
          FileName: row
        }
      }
      const res = await connrequest('ImportExactEMail', data)
      if(res.Status.StatusCode === 0){
        this.$message.success('操作成功')
        this.GetShareFiles(this.Dir)
      }
    },
    handleClick(tab) {
      this.Dir = tab.label
      this.GetShareFiles(tab.label)
    }
  }
};
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>